package com.example.dzbsh.controller;

import java.util.List;
import java.util.UUID;

import com.example.dzbsh.model.Dept;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.dzbsh.service.DeptService;
import com.example.dzbsh.util.BooleanMessage;
import com.example.dzbsh.util.JsonResult;
import com.example.dzbsh.util.StringHandle;

import javax.annotation.Resource;


@RestController
@RequestMapping("dept")
public class DeptController {

    @Resource
    DeptService ddDeptService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    //跟据单位查询下级单位 层级缩进
    @RequestMapping("findcengji_by_deptid")
    public JsonResult<?> findCengjiByDeptId(@RequestParam("deptid") String ppDeptId) {
        try {
            return JsonResult.getSuccessResult(ddDeptService.findCengjiByDeptId(ppDeptId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("DeptController->findcengji_by_deptid:"+e.getMessage());
            return JsonResult.getErrorResult("根据上级单位查询所有下级单位失败");
        }
    }

    //跟据单位查询下级单位
    @RequestMapping("find_by_parentdeptid")
    public JsonResult<?> findByParentdeptID(@RequestParam("parentdeptid") String ppParentdeptID) {
        try {
            return JsonResult.getSuccessResult(ddDeptService.findByParentdeptId(ppParentdeptID));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("DeptController->findByParentdeptID:"+e.getMessage());
            return JsonResult.getErrorResult("根据上级单位查询单位失败");
        }
    }

    //跟据单位查询下级单位
    @RequestMapping("findxiaji_by_deptid")
    public JsonResult<?> findXiajiByDeptId(@RequestParam("deptid") String ppDeptId) {
        try {
            return JsonResult.getSuccessResult(ddDeptService.findXiajiByDeptId(ppDeptId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("DeptController->findxiaji_by_deptid:"+e.getMessage());
            return JsonResult.getErrorResult("根据上单位查询所有下级单位失败");
        }
    }
    @RequestMapping("find_all")
    public JsonResult<?> find_all(){
        try {
            return JsonResult.getSuccessResult(ddDeptService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("DeptController->find_all:"+e.getMessage());
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    @RequestMapping("find_one")
    public JsonResult<?> find_one(
            @RequestParam("deptid") String ppDeptId
    ){
        try {
            return JsonResult.getSuccessResult(ddDeptService.findOne(ppDeptId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("DeptController->find_one:"+e.getMessage());
            return JsonResult.getErrorResult("单个单位查询失败");
        }
    }

    @RequestMapping("add")
    public JsonResult<?> add(
            @RequestParam("parentdeptid") String ppParentdeptid,
            @RequestParam("danweiduiwaimingcheng") String ppDanweiduiwaimingcheng,
            @RequestParam("danweiduineimingcheng") String ppDanweiduineimingcheng,
            @RequestParam("isvalid") int ppIsvalid,
            @RequestParam("xuliebianma") String ppXuliebianma,
            @RequestParam("jigoumingchengma") String ppJigoumingchengma
    ){
        try {
        	if (ddDeptService.findByDuiwaimingcheng(ppDanweiduiwaimingcheng).size()>0) {
				return JsonResult.getErrorResult("已存在该单位对外名称，修改其他单位对外名称");
			}
            if (ddDeptService.findByXuliebianma(ppXuliebianma).size()>0){
                return JsonResult.getErrorResult("已存在该序列编码，修改其他序列编码");
            }
            if (ddDeptService.findByJigoumingchengma(ppJigoumingchengma).size()>0){
                return JsonResult.getErrorResult("已存在该机构名称码，修改其他机构名称码");
            }
            Dept mmDept=new Dept();
            mmDept.setDeptid(UUID.randomUUID().toString());
            BooleanMessage  mmBooleanMessage=checkInputData(mmDept,ppParentdeptid,ppDanweiduiwaimingcheng,ppDanweiduineimingcheng,ppIsvalid,ppXuliebianma,ppJigoumingchengma);
            if (!mmBooleanMessage.isOk()) {
                return JsonResult.getErrorResult(mmBooleanMessage.getMessage().toString());
            }
            mmDept = (Dept) mmBooleanMessage.getMessage();

            return ddDeptService.add(mmDept) == true ? JsonResult
                    .getSuccessResult("添加成功") : JsonResult.getErrorResult("添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("DeptController->add:"+e.getMessage());
            return JsonResult.getErrorResult("单位添加失败");
        }
    }

    @RequestMapping("modify")
    public JsonResult<?> modify(
            @RequestParam("deptid") String ppDeptId,
            @RequestParam("danweiduiwaimingcheng") String ppDanweiduiwaimingcheng,
            @RequestParam("danweiduineimingcheng") String ppDanweiduineimingcheng,
            @RequestParam("isvalid") int ppIsvalid,
            @RequestParam("xuliebianma") String ppXuliebianma,
            @RequestParam("jigoumingchengma") String ppJigoumingchengma
    ){
        try {

            Dept mmDept=ddDeptService.findOne(ppDeptId);
            if (mmDept==null){
                return JsonResult.getErrorResult("暂无该单位");
            }
            if (ddDeptService.findExitByDuiwaimingcheng(ppDanweiduiwaimingcheng, ppDeptId).size()>0) {
				return JsonResult.getErrorResult("已存在该单位对外名称，修改其他单位对外名称");
			}
            if (ddDeptService.findExitByXuliebianma(ppXuliebianma,ppDeptId).size()>0){
                return JsonResult.getErrorResult("已存在该序列编码，修改其他序列编码");
            }
            if (ddDeptService.findExitByJigoumingchengma(ppJigoumingchengma,ppDeptId).size()>0){
                return JsonResult.getErrorResult("已存在该机构名称码，修改其他机构名称码");
            }
            BooleanMessage  mmBooleanMessage=checkInputData(mmDept,mmDept.getParentdeptid(),ppDanweiduiwaimingcheng,ppDanweiduineimingcheng,ppIsvalid,ppXuliebianma,ppJigoumingchengma);
            if (!mmBooleanMessage.isOk()) {
                return JsonResult.getErrorResult(mmBooleanMessage.getMessage().toString());
            }
            mmDept = (Dept) mmBooleanMessage.getMessage();

            return ddDeptService.modify(mmDept) == true ? JsonResult
                    .getSuccessResult("修改成功") : JsonResult.getErrorResult("修改失败");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("DeptController->modify:"+e.getMessage());
            return JsonResult.getErrorResult("单位修改失败");
        }
    }

    // 删除单个单位
    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("deptid") String ppDeptId) {
        try {
            if ("".equals(ppDeptId)) {
                return JsonResult.getErrorResult("单位id不能为空");
            }

            Dept mmDept=ddDeptService.findOne(ppDeptId);
            if (mmDept == null) {
                return JsonResult.getErrorResult("要删除的单位不存在");
            }
            List<Dept> mmDeptList = ddDeptService.findByParentdeptId(ppDeptId);
            if (mmDeptList.size()>0){
                return JsonResult.getErrorResult("该单位存在下级单位，请先删除下级单位");
            }
            String mmParentDeptid = mmDept.getParentdeptid()==null?"":mmDept.getParentdeptid();
            if ("".equals(mmParentDeptid)){
                return JsonResult.getErrorResult("最高层级单位，不可删除");
            }

            return ddDeptService.delete(ppDeptId) == true ? JsonResult.getSuccessResult("删除成功") : JsonResult.getErrorResult("删除失败");
        } catch (Exception e) {
            logger.error("DeptController->delete:"+e.getMessage());
            return JsonResult.getErrorResult("单位删除失败");
        }
    }

    private BooleanMessage checkInputData(Dept mmDept,
                                          String ppParentdeptid,String ppDanweiduiwaimingcheng,
                                          String ppDanweiduineimingcheng,
                                          int ppIsvalid, String ppXuliebianma,String ppJigoumingchengma) {
        ppParentdeptid=ppParentdeptid.trim();
        ppDanweiduiwaimingcheng=ppDanweiduiwaimingcheng.trim();
        ppDanweiduineimingcheng=ppDanweiduineimingcheng.trim();
        ppXuliebianma=ppXuliebianma.trim();
        ppJigoumingchengma=ppJigoumingchengma.trim();

        if ("".equals(ppParentdeptid)) {
            return BooleanMessage.getErrorMessage("上级单位id不能为空");
        }
        if ("".equals(ppIsvalid)) {
            return BooleanMessage.getErrorMessage("是否有效不能为空");
        }
        if (StringHandle.getStringByteLength(ppParentdeptid) > 50) {
            return BooleanMessage.getErrorMessage("上级单位id不能超过50字节");
        }
        if (StringHandle.getStringByteLength(ppDanweiduiwaimingcheng) > 200) {
            return BooleanMessage.getErrorMessage("对外单位名称不能超过200字节");
        }
        if (StringHandle.getStringByteLength(ppDanweiduineimingcheng) > 200) {
            return BooleanMessage.getErrorMessage("对内单位名称不能超过200字节");
        }
        if (StringHandle.getStringByteLength(String.valueOf(ppIsvalid)) > 50) {
            return BooleanMessage.getErrorMessage("是否有效不能超过11字节");
        }
        if (StringHandle.getStringByteLength(ppXuliebianma) > 200) {
            return BooleanMessage.getErrorMessage("序列编码不能超过200字节");
        }
        if (StringHandle.getStringByteLength(ppJigoumingchengma) > 255) {
            return BooleanMessage.getErrorMessage("机构名称码不能超过255字节");
        }


        mmDept.setParentdeptid(ppParentdeptid);
        mmDept.setDanweiduiwaimingcheng(ppDanweiduiwaimingcheng);
        mmDept.setDanweiduineimingcheng(ppDanweiduineimingcheng);
        mmDept.setOrdernum(ddDeptService.getMaxOrderNum(ppParentdeptid)+1);
        mmDept.setIsvalid(ppIsvalid);
        mmDept.setXuliebianma(ppXuliebianma);
        mmDept.setJigoumingchengma(ppJigoumingchengma);
        return BooleanMessage.getSuccessMessage(mmDept);
    }

}
